package br.com.infocruz.seam.crm.ejb.session;

import br.com.infocruz.seam.crm.ejb.model.*;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("customerHome")
public class CustomerHome extends EntityHome<Customer> {

	@In(create = true)
	AddressHome addressHome;
	@In(create = true)
	CompanyHome companyHome;

	public void setCustomerId(Long id) {
		setId(id);
	}

	public Long getCustomerId() {
		return (Long) getId();
	}

	@Override
	protected Customer createInstance() {
		Customer customer = new Customer();
		return customer;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Address address = addressHome.getDefinedInstance();
		if (address != null) {
			getInstance().setAddress(address);
		}
		Company company = companyHome.getDefinedInstance();
		if (company != null) {
			getInstance().setCompany(company);
		}
	}

	public boolean isWired() {
		return true;
	}

	public Customer getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

}
